package com.appistry.management;

import java.net.InetAddress;
import java.net.UnknownHostException;

import com.appistry.api.rest.AbstractManager;
import com.appistry.api.rest.RestConnection;
import com.appistry.api.rest.domain.Credentials;
import com.appistry.api.util.Version;
import com.appistry.management.domain.impl.FabricImpl;
import com.appistry.management.rest.impl.RestFabricManagerImpl;

public class FabricManager extends AbstractManager {
	private static final int DEFAULT_HTTP_PORT = 16080;
	private static final int DEFAULT_HTTPS_PORT = 16443;
	private Fabric fabric;
	private RestConnection restConnection;

	public FabricManager(String user, String password, String ip, Version version) throws UnknownHostException {
		this(user, password, ip, DEFAULT_HTTP_PORT, false, version);
	}

	public FabricManager(String user, String password, String ip, boolean isSSL, Version version) throws UnknownHostException {
		int port = DEFAULT_HTTP_PORT;

		if (isSSL) {
			port = DEFAULT_HTTPS_PORT;
		}

		InetAddress address = InetAddress.getByName(ip);
		Credentials credentials = new Credentials(user, password);
		RestFabricManagerImpl manager = new RestFabricManagerImpl(address, port, isSSL, credentials, version);

		this.restConnection = manager.getRestConnection();
		this.fabric = new FabricImpl(manager);
	}

	public FabricManager(String user, String password, String ip, int port, Version version) throws UnknownHostException {
		this(user, password, ip, port, false, version);
	}

	public FabricManager(String user, String password, String ip, int port, boolean isSSL, Version version) throws UnknownHostException {
		InetAddress address = InetAddress.getByName(ip);
		Credentials credentials = new Credentials(user, password);
		RestFabricManagerImpl manager = new RestFabricManagerImpl(address, port, isSSL, credentials, version);

		this.restConnection = manager.getRestConnection();
		this.fabric = new FabricImpl(manager);
	}

	public FabricManager(Credentials credentials, InetAddress address, int port, boolean isSSL, Version version) {
		RestFabricManagerImpl manager = new RestFabricManagerImpl(address, port, isSSL, credentials, version);

		this.restConnection = manager.getRestConnection();
		this.fabric = new FabricImpl(manager);
	}

	public Fabric getFabric() {
		return fabric;
	}

	@Override
	protected RestConnection getRestConnection() {
		return restConnection;
	}
	
}
